;(function(){
'use strict';
angular.module('services.usermanager.userInfoServer',[])
  .factory('userInfoRequest',userInfoRequest)
  .factory('userStaticData',userStaticData)
  .factory('userInfoFunction', userInfoFunction);        //数据

function userInfoRequest(Restangular){
  return {
    pwdUpdate : function(params,callback){
      return Restangular.all('sas/sysMng/user/pwdModify').post(params).then(callback);
    },
    checkOldPwd: function(params,callback){
      return Restangular.all('sas/sysMng/user/pwdCheck').post(params).then(callback);
    }
  };  
}

function userStaticData ($cookies){
  var initData = {};
  return {
    changeData:initData,
    initUserData:initUserData
  };

  function initUserData(){
    initData.data = $cookies.getObject('userCookie');
    return initData;
  }
}




//用户修改信息方法集合
function userInfoFunction($mdDialog,userStaticData) {
  var allFunction = {
    updateUserInfo:updateUserInfo,
    updateUserPwd:updateUserPwd,
    setData: setData
  };
  return allFunction;
  //修改用户信息
  function updateUserInfo(ev){
    $mdDialog.show({
      controller: updateUserInfoCtrl,
      templateUrl: 'views/sysMng/userDetail/userDetailDialog/updateUserInfo.html',
      targetEvent: ev
    })
    .then(updateSave);

  }

  //修改用户信息密码
  function updateUserInfoCtrl($scope,$mdDialog,userStaticData,userInfoFunction,userRequest){
    $scope.userData2 = angular.copy(userStaticData.changeData.data);
     
    //取消弹出框
    $scope.cancel = function() {
      $mdDialog.cancel();
    }; 
    /**
     * [infoSave 修改保存]
     * @param  {[type]} flag [description]
     * @return {[type]}      [description]
     */
    $scope.infoSave = function(flag) {
      if(flag){ //if form invalid ,not submitted
        return;
      }
      userInfoFunction.setData($scope.userData2);
      userRequest.updateSave(userStaticData.changeData.data,function(res){
        if(res.state === '10000'){
          alert('修改成功');
        }else{
          alert('修改失败');
        }
      });
      //保存请求
      $mdDialog.hide();
    };
  }

  updateUserInfoCtrl.$inject = ['$scope','$mdDialog','userStaticData','userInfoFunction','userRequest'];


  function setData(data){
      var  newData = userStaticData.changeData.data;
      newData.sex = data.sex;
      newData.userName = data.userName;
      newData.realName = data.realName;
      newData.idCard = data.idCard;
      newData.phone = data.phone;
      newData.email = data.email;
      newData.updateId = newData.userId;
  }
  //修改信息保存
  function updateSave (){}
  //修改用户密码
  function updateUserPwd(ev){
     $mdDialog.show({
      controller: updateUserPwdCtrl,
      templateUrl: 'views/sysMng/userDetail/userDetailDialog/updatePwd.html',
      targetEvent: ev
    })
    .then(updatePwdSave);
  } 
  function updatePwdSave(){}
  //修改密码弹出框控制器
  function updateUserPwdCtrl($scope,$mdDialog,userStaticData,CoreService,userInfoRequest,$cookies,$state){
      $scope.userInfo = {
        newPWD:'',
        oldPWD:'',
        ensurePwd:''
      };
      $scope.cancel = function() {
      $mdDialog.cancel();
    };  


    /**
     * [infoSave 修改保存]
     * @param  {[type]} flag [description]
     * @return {[type]}      [description]
     */
    $scope.infoSave = function(flag) {      
      var check = $scope.userInfo.newPWD !== $scope.userInfo.ensurePwd;
      var params = {
        oldPWD:$scope.userInfo.oldPWD,
        newPWD:$scope.userInfo.newPWD,
        userId:$cookies.getObject('userCookie').userId,
        userName:$cookies.getObject('userCookie').userName
      };

      userInfoRequest.pwdUpdate(params,function(res){
          if(res.state === '10000' ){
             if(check){
               alert('请输入正确密码');
               return;
            }
            $mdDialog.hide();
            alert('密码修改成功');
            $state.go('login');
            $cookies.remove('userCookie');
          }else{
            alert('旧密码错误或系统错误');
        }

      });
  
    
      
    };

  }
  updateUserPwdCtrl.$inject = ['$scope','$mdDialog','userStaticData','CoreService','userInfoRequest','$cookies','$state'];
}
userInfoFunction.$inject = ['$mdDialog','userStaticData'];

})();
